package com.asia5b.a5mall.middleware.widget.refresh

import android.view.View
import android.view.animation.Animation
import android.view.animation.LinearInterpolator
import android.view.animation.ScaleAnimation
import android.widget.ImageView
import androidx.coordinatorlayout.widget.CoordinatorLayout
import com.asia5b.a5mall.middleware.R
import com.asia5b.a5mall.middleware.utils.animSequentialStart
import com.asia5b.a5mall.middleware.utils.judge

/**
 * Created by zhangyuncai on 2019/1/28.
 */
object AnimationHelper {
    // FAB移出屏幕动画（隐藏动画）
    fun animateOut(fab: View) {
        val layoutParams = fab.layoutParams as CoordinatorLayout.LayoutParams
        val bottomMargin = layoutParams.bottomMargin
        fab.animate().translationY(fab.height + bottomMargin.toFloat()).setInterpolator(LinearInterpolator()).start()
    }

    // FAB移入屏幕动画（显示动画）
    fun animateIn(fab: View) {
        fab.animate().translationY(0f).setInterpolator(LinearInterpolator()).start()
    }


    /**
     * @Note   切换列表布局 带动画效果
     * @param  imageView 动画视图
     * @param  isSingle true/单列
     */
     fun reclyTypeAnim(imageView: ImageView, isSingle: Boolean = false) {
        val narrowAnimation = ScaleAnimation(1f, 0.1f, 1f, 0.1f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f)
        val enlargeanimation = ScaleAnimation(
                0.1f, 1.0f, 0.1f, 1.0f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f
        )

        /**0.6秒完成动画 */
        narrowAnimation.duration = 300
        enlargeanimation.duration = 300

        /**顺序执行动画，并且设置拦截器*/
        imageView.animSequentialStart(arrayListOf(narrowAnimation, enlargeanimation), interceptor = { index, state ->
            if (state == 2 && index == 0) {
                imageView.setImageResource(isSingle.judge(R.drawable.ic_recy_list, R.drawable.ic_recy_grid))
            }
            return@animSequentialStart false
        })
    }
}